//
//  CountDownView.swift
//  CalligraphyUniversal
//
//  Created by 李雷川 on 2017/9/27.
//Copyright © 2017年 李雷川. All rights reserved.
//

import UIKit
import FZSFCommonTools
class CountDownView: UIView {
    

    fileprivate let backgroundView = UIView()
    fileprivate let label = UILabel.createLabel(textColor: AppColors.highlight, font: UIFont.systemFont(ofSize: 100), defaultText: "3")
    fileprivate  var second = 3
    open var finishClosure:(()->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundView.backgroundColor = UIColor.white
        addSubview(backgroundView)
        backgroundView.addSubview(label)
        backgroundView.snp.makeConstraints { (make) in
            make.size.equalTo(117)
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-30)
        }
        
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(backgroundView)
            make.center.equalTo(backgroundView)
        }
    }
    
    deinit {
        debugPrint("CountDownView deinit")
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func start(){
        self.backgroundView.transform = .identity
        self.backgroundView.alpha = 0.8
        label.text = "\(second)"
        UIView.animate(withDuration: 1.0, animations: {
            self.backgroundView.transform = .init(scaleX: 0.6, y: 0.6)
            self.backgroundView.alpha = 0.4
        }) { (finished) in
            if(self.second == 1){
                if let doClosure = self.finishClosure{
                    doClosure()
                }
                self.removeFromSuperview()
            }
            else{
                self.second = self.second - 1
                self.start()
            }
        }
    }
    
}
